/*
 * 文件名：merchantTypeController.java	 
 * 时     间：下午4:20:10
 * 作     者：liyf       
 * 版     权： 2014-2022  智慧园区, 公司保留所有权利.
 * 联     系：www.szyungu.com
 */
package com.cz073586.technology.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.cz073586.common.contants.CommonContans;
import com.cz073586.common.controller.BaseController;
import com.cz073586.common.entity.pojo.PageInfo;
import com.cz073586.technology.model.AdInfo;
import com.cz073586.technology.model.MerchantType;
import com.cz073586.technology.service.face.AdInfoService;
import com.cz073586.technology.service.face.MerchantTypeService;
import com.cz073586.tools.privilege.ResourceAndOperate;
import com.cz073586.tools.util.UUIDGenerateUtil;

/**
 * @ClassName: merchantTypeController
 * @Description: 商家类型信息
 * @author zengqj
 * @date 2015年1月20日 下午4:20:10
 */
@RestController
@RequestMapping(value = "/merchantTypeManager", method = RequestMethod.POST)
@ResourceAndOperate(moduleID = "com.technology.merchantTypeManager", moduleName = "商家类型管理", resouceDesc = "商家类型管理", parentModuleID = "com.technology", parentModuleName = "昱显科技项目")
public class MerchantTypeController extends BaseController
{
	@Autowired
	private MerchantTypeService merchantTypeService;
	
	@Autowired
	private AdInfoService adInfoService;
	
	/**
	 * 
	 * @方法名称: queryServicesByPage
	 * @描述: 查询商家类型列表
	 *
	 * @参数:
	 * @param page
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:zengqj
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryMerchantTypeByPage.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(moduleID = "com.technology.merchantTypeManager.querymerchantTypeListByPage", moduleName = "查询", resouceDesc = "分页查询", parentModuleID = "com.technology.merchantTypeManager", parentModuleName = "商家类型管理")
	public ModelMap queryMerchantTypeByPage(
			@RequestBody PageInfo<MerchantType> page)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			 merchantTypeService.queryByPage(page);
			resultMap.put(CommonContans.STRUTS_DATA,page);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/queryParentMerchantType.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(moduleID = "com.technology.merchantTypeManager.queryParentMerchantType", 
			moduleName = "查询类型的父类信息", resouceDesc = "查询类型的父类信息", 
			parentModuleID = "com.technology.merchantTypeManager", parentModuleName = "商家类型管理")
	public ModelMap queryParentMerchantType(
			@RequestBody MerchantType merchantType)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			merchantType.setTypeStrategy(3);
			resultMap.put(CommonContans.STRUTS_DATA, merchantTypeService.queryByMerchantType(merchantType));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/queryMerchantTypeList.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(moduleID = "com.technology.merchantTypeManager.queryMerchantTypeList", 
			moduleName = "条件查询商家类型，返回集合", resouceDesc = "条件查询商家类型，返回集合", 
			parentModuleID = "com.technology.merchantTypeManager", parentModuleName = "商家类型管理")
	public ModelMap queryMerchantTypeList(
			@RequestBody MerchantType merchantType)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			resultMap.put(CommonContans.STRUTS_DATA, merchantTypeService.queryByMerchantType(merchantType));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/queryHomePage.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(moduleID = "com.technology.merchantTypeManager.queryHomePage", moduleName = "查询", resouceDesc = "主页信息，无入参直接传{}即可", parentModuleID = "com.technology.merchantTypeManager", parentModuleName = "商家类型管理")
	public ModelMap queryHomePage(MerchantType merchantType)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			List<MerchantType> merchantTypeList = merchantTypeService
					.queryHomePage(merchantType);
			for (MerchantType mt : merchantTypeList)
			{
				merchantType = new MerchantType();
				merchantType.setParentId(mt.getUuid());
				List<MerchantType> mTypeList = merchantTypeService
						.queryMerchantTypeByParentId(merchantType);
				if (CollectionUtils.isNotEmpty(mTypeList))
				{
					mt.setListMerchantType(mTypeList);
				}
				if (mt.getTypeStrategy() == 0)
				{
					String[] adIds = mt.getBindToId().split(",");
					List<AdInfo> listAdInfo = new ArrayList<AdInfo>();
					for (String adId : adIds)
					{
						AdInfo adInfo = new AdInfo();
						adInfo = adInfoService.selectByPrimaryKey(adId);
						listAdInfo.add(adInfo);
					}
					mt.setListAdInfo(listAdInfo);
				}
			}
			resultMap.put(CommonContans.STRUTS_DATA, merchantTypeList);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/querymerchantTypeAd.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(moduleID = "com.technology.merchantTypeManager.querymerchantTypeAd", moduleName = "查询", resouceDesc = "入参：无返回：该滑屏和词条广告与建表时唯一的城市父类型绑定", parentModuleID = "com.technology.merchantTypeManager", parentModuleName = "商家类型管理")
	public ModelMap querymerchantTypeAd()
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			MerchantType merchantType = new MerchantType();
			List<MerchantType> merchantTypeList = merchantTypeService
					.queryByMerchantType(merchantType);
			for (MerchantType mt : merchantTypeList)
			{
				if (0 == mt.getTypeStrategy())
				{
					String[] adIds = mt.getBindToId().split(",");
					List<AdInfo> listAdInfo = new ArrayList<AdInfo>();
					for (String adId : adIds)
					{
						AdInfo adInfo = new AdInfo();
						adInfo = adInfoService.selectByPrimaryKey(adId);
						listAdInfo.add(adInfo);
					}
					mt.setListAdInfo(listAdInfo);
				}
			}
			resultMap.put(CommonContans.STRUTS_DATA, merchantTypeList);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: querymerchantTypeById
	 * @描述: 通过商家类型ID查询
	 *
	 * @参数:
	 * @param merchantTypeId
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:zengqj
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryMerchantTypeById.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(moduleID = "com.technology.merchantTypeManager.queryMerchantTypeById", moduleName = "查询单一对象", resouceDesc = "", parentModuleID = "com.technology.merchantTypeManager", parentModuleName = "商家类型管理")
	public ModelMap queryMerchantTypeById(String merchantTypeId)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			MerchantType merchantType = merchantTypeService.selectByPrimaryKey(merchantTypeId);
			String adInfos = merchantType.getBindToId();
//			String adInfos = merchantType.getBindToId();
			List<AdInfo> listAdInfo = adInfoService.queryAdByIdsIn(adInfos.split(","));
			List<AdInfo> listUnAdInfo = adInfoService.queryAdByIdsNotIn(adInfos.split(","));
			if(CollectionUtils.isNotEmpty(listAdInfo))
			{
				merchantType.setListAdInfo(listAdInfo);
			}
			if(CollectionUtils.isNotEmpty(listUnAdInfo))
			{
				merchantType.setListUnAdInfo(listUnAdInfo);
			}
			resultMap.put(CommonContans.STRUTS_DATA,merchantType);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：添加商家类型信息，uuid由代码写入
	 * 
	 * @Title: insertmerchantType
	 * @param @param merchantType
	 * @param @return
	 * @return ModelMap
	 * @merchantType merchantTypeministrator 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/insertMerchantType.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(moduleID = "com.technology.merchantTypeManager.insertMerchantType", moduleName = "添加", resouceDesc = "", parentModuleID = "com.technology.merchantTypeManager", parentModuleName = "商家类型管理")
	public ModelMap insertMerchantType(@RequestBody MerchantType merchantType)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			merchantType.setUuid(UUIDGenerateUtil.generate());
			resultMap.put(CommonContans.STRUTS_DATA,
					merchantTypeService.insert(merchantType));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：根据merchantType主机修改商家类型信息
	 * 
	 * @Title: updatemerchantType
	 * @param @param merchantType
	 * @param @return
	 * @return ModelMap
	 * @merchantType merchantTypeministrator 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/updateMerchantType.action",method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(moduleID = "com.technology.merchantTypeManager.updateMerchantType", moduleName = "修改", resouceDesc = "", parentModuleID = "com.technology.merchantTypeManager", parentModuleName = "商家类型管理")
	public ModelMap updateMerchantType(@RequestBody MerchantType merchantType)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			resultMap.put(CommonContans.STRUTS_DATA,
					merchantTypeService.updateByPrimaryKeySelective(merchantType));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：根据主键删除商家类型
	 * 
	 * @Title: deletemerchantType
	 * @param @param merchantTypeId
	 * @param @return
	 * @return ModelMap
	 * @merchantType merchantTypeministrator 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/deleteMerchantType.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(moduleID = "com.technology.merchantTypeManager.deleteMerchantType", moduleName = "删除", resouceDesc = "", parentModuleID = "com.technology.merchantTypeManager", parentModuleName = "商家类型管理")
	public ModelMap deleteMerchantType(@RequestBody MerchantType merchantType)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			if(StringUtils.isBlank(merchantType.getUuid()))
			{
				resultMap.put(CommonContans.STRUTS_DATA,"merchantType uuid is empty..");
				resultMap.put(CommonContans.STRUTS_RESULT,
						CommonContans.STRUTS_ERROR);
				return resultMap;
			}
			resultMap.put(CommonContans.STRUTS_DATA,
					merchantTypeService.deleteByPrimaryKey(merchantType.getUuid()));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/addMerchantType.action", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(moduleID = "com.technology.merchantTypeManager.addMerchantType", moduleName = "添加商家类型", resouceDesc = "商家类型添加，带附件的", parentModuleID = "com.technology.merchantTypeManager", parentModuleName = "商家类型管理")
	public ModelAndView addMerchantType(MerchantType merchantType,
			@RequestParam MultipartFile[] myfiles, HttpServletRequest request)
			throws IOException
	{
		
		ModelMap resultMap = new ModelMap();
		merchantType.setUuid(UUIDGenerateUtil.generate());
		try
		{
			// 如果只是上传一个文件，则只需要MultipartFile类型接收文件即可，而且无需显式指定@RequestParam注解
			// 如果想上传多个文件，那么这里就要用MultipartFile[]类型来接收文件，并且还要指定@RequestParam注解
			// 并且上传多个文件时，前台表单中的所有<input
			// type="file"/>的name都应该是myfiles，否则参数里的myfiles无法获取到所有上传的文件
			if (merchantType.getTypeStrategy() == 1)
			{
				String file = "";
				for (MultipartFile myfile : myfiles)
				{
					if (myfile.isEmpty())
					{
						System.out.println("文件未上传");
					} else
					{
						System.out.println("文件长度: " + myfile.getSize());
						System.out.println("文件类型: " + myfile.getContentType());
						System.out.println("文件名称: " + myfile.getName());
						System.out.println("文件原名: "
								+ myfile.getOriginalFilename());
						System.out
								.println("========================================");
						// 如果用的是Tomcat服务器，则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中
						String realPath = request.getSession()
								.getServletContext()
								.getRealPath("/image/merchanttype");
						// 这里不必处理IO流关闭的问题，因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉，我是看它的源码才知道的
						String fileName = System.currentTimeMillis()
								+ myfile.getOriginalFilename();
						file = "/image/merchanttype/" + fileName;
						FileUtils.copyInputStreamToFile(
								myfile.getInputStream(), new File(realPath,
										fileName));
						System.out.println(fileName);
					}
					merchantType.setImgUrl(file);
				}
			}
			merchantTypeService.insert(merchantType);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		// return resultMap;
		return new ModelAndView(
				"redirect:/admin/view/technology/merchanttype/merchanttypelist.jsp");
	}
	
	
	@RequestMapping(value = "/updateMerchantTypeFile.action", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(moduleID = "com.technology.merchantTypeManager.updateMerchantTypeFile", 
						moduleName = "修改商家类型", 
						resouceDesc = "商家类型修改，带附件的", 
						parentModuleID = "com.technology.merchantTypeManager", 
						parentModuleName = "商家类型管理")
	public ModelAndView updateMerchantTypeFile(MerchantType merchantType,
			@RequestParam MultipartFile[] myfiles, HttpServletRequest request)
			throws IOException
	{
		
		ModelMap resultMap = new ModelMap();
		try
		{
			// 如果只是上传一个文件，则只需要MultipartFile类型接收文件即可，而且无需显式指定@RequestParam注解
			// 如果想上传多个文件，那么这里就要用MultipartFile[]类型来接收文件，并且还要指定@RequestParam注解
			// 并且上传多个文件时，前台表单中的所有<input
			// type="file"/>的name都应该是myfiles，否则参数里的myfiles无法获取到所有上传的文件
			if (merchantType.getTypeStrategy() == 1)
			{
				String file = "";
				for (MultipartFile myfile : myfiles)
				{
					if (myfile.isEmpty())
					{
						System.out.println("文件未上传");
					} else
					{
						System.out.println("文件长度: " + myfile.getSize());
						System.out.println("文件类型: " + myfile.getContentType());
						System.out.println("文件名称: " + myfile.getName());
						System.out.println("文件原名: "
								+ myfile.getOriginalFilename());
						System.out
								.println("========================================");
						// 如果用的是Tomcat服务器，则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中
						String realPath = request.getSession()
								.getServletContext()
								.getRealPath("/image/merchanttype");
						// 这里不必处理IO流关闭的问题，因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉，我是看它的源码才知道的
						String fileName = System.currentTimeMillis()
								+ myfile.getOriginalFilename();
						file = "/image/merchanttype/" + fileName;
						FileUtils.copyInputStreamToFile(
								myfile.getInputStream(), new File(realPath,
										fileName));
						System.out.println(fileName);
					}
					if(StringUtils.isNotBlank(file))
					{
						merchantType.setImgUrl(file);
					}
				}
			}
			merchantTypeService.updateByPrimaryKeySelective(merchantType);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		// return resultMap;
		return new ModelAndView(
				"redirect:/admin/view/technology/merchanttype/merchanttypelist.jsp");
	}
}
